// controller/user.js
// const uniID = require('uni-id')
const Controller = require('uni-cloud-router').Controller;
const {validateToken} = require('validdate-token');
// 必须继承 Controller
module.exports = class DisposeDataController extends Controller {
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-09-12
	 * 给项目分配钻机
	 */
	async allotDrillInProject() {
		let {
			projectID,
			drillID
		} = this.ctx.data;
		return this.service.disposeData.allotDrillInProject({
			projectID,
			drillID
		});
	}
	/**
	 * 2020-11-17
	 * 给项目分配员工
	 */
	async allotWorkerInProject() {
		let {
			projectID,
			worker_ids,
			salaryWay
		} = this.ctx.data;
		return this.service.disposeData.allotWorkerInProject({
			projectID,
			worker_ids,
			salaryWay
		});
	}
	/**
	 * 验证钻机ID的分配
	 * 2020-08-08
	 */
	async checkDrillID() {
		let {
			id
		} = this.ctx.data;
		return this.service.disposeData.checkDrillID({
			id
		});
	}
	/**
	 * 验证配件编号的分配
	 * 2020-08-08
	 */
	async checkPartsID() {
		let {
			id
		} = this.ctx.data;
		return this.service.disposeData.checkPartsID({
			id
		});
	}
	/**
	 * 2020-08-15
	 * 验证项目编号
	 */
	async checkProjectID() {
		let {
			id
		} = this.ctx.data;
		return this.service.disposeData.checkProjectID({
			id
		});
	}
	/**
	 * 2020-07-26
	 * 验证员工ID是否重复
	 */
	async checkWorkID() {
		let {
			id
		} = this.ctx.data;
		return this.service.disposeData.checkWorkID({
			id
		});
	}
	/**
	 * 2020-09-19
	 * 删除项目里面的钻机
	 * ----（分配钻机时钻机已在其他项目里面但是任然选择分配）
	 */
	async deleteDrillInProject() {
		let {
			drill_id
		} = this.ctx.data;
		return this.service.disposeData.deleteDrillInProject({
			drill_id
		});
	}
	/**
	 * 获取钻机信息
	 * 2020-08-09
	 */
	async getDrillInfo() {
		return this.service.disposeData.getDrillInfo();
	}
	/**
	 * 根据公司ID获取 员工信息
	 * 2021-6-9
	 */
	async getDrillInfoByPmCompany() {
		let {token,pm_company} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.disposeData.getDrillInfoByPmCompany(pm_company);
		}
	}
	
	/**
	 * 添加/编辑 钻机信息
	 * 2020-08-08
	 */
	async setDrillInfo() {
		let {
			drill_id,
			drill_price,
			drill_buy_date,
			drill_status,
			maintain_record,
			pm_company
		} = this.ctx.data;
		return this.service.disposeData.setDrillInfo({
			drill_id,
			drill_price,
			drill_buy_date,
			drill_status,
			maintain_record,
			pm_company
		});
	}
	/**
	 * 通过ID获取单个钻机信息
	 * 2020-08-09
	 */
	async getOneDrillInfo() {
		let {
			_id
		} = this.ctx.data;
		return this.service.disposeData.getOneDrillInfo({
			_id
		});
	}
	/**
	 * 编辑/添加 配件信息
	 * 2020-08-09
	 */
	async setPartsInfo() {
		let {
			parts_type,
			buy_date,
			buy_address,
			buy_price,
			parts_id,
			parts_receipt //票据
		} = this.ctx.data;
		return this.service.disposeData.setPartsInfo({
			parts_type,
			buy_date,
			buy_address,
			buy_price,
			parts_id,
			parts_receipt //票据
		});
	}
	/**
	 * 通过ID获取配件信息
	 * 2020-08-09
	 */
	async getPartsInID() {
		let {
			_id
		} = this.ctx.data;
		return this.service.disposeData.getPartsInID({
			_id
		});
	}
	/**
	 * 获取配件信息
	 * 2020-08-09
	 */
	async getPartsInfo(){
		return this.service.disposeData.getPartsInfo();
	}
}
